'''
@Problem description:
Determine whether an integer is a palindrome. Do this without extra space.
确定整数是否是回文数，并且没有额外的空间消耗

@For example:
21312,22是回文数
'''


class Solution:
    '''
    每次取该数的头尾进行比较，比较完掐头去尾
    '''

    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if (x < 0):
            return False
        t = x
        digit = 1
        while (t >= 10):
            t = int(t / 10)
            digit *= 10
        while (digit > 1):
            high = int(x / digit)
            low = x % 10
            if (high != low):
                return False
            x = (x - (high * digit) - low) / 10
            digit /= 100
        return True

    '''
    从该数的尾部(个位数)开始，按位算出反过来的数值，和原数值比较
    '''

    def isPalindrome2(self, x):
        t = x
        if (t < 0 or (int(t % 10) == 0 and t != 0)):
            return False
        revertedNumber = 0
        while (t > 0):
            revertedNumber = revertedNumber * 10 + t % 10
            t = int(t / 10)
        return x == revertedNumber
